package com.neu.ui.dashboard;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.neu.R;
import com.neu.adapter.NewsAdapter;
import com.neu.bean.Category;
import com.neu.bean.News;
import com.neu.ui.BaseFragment;

import java.util.List;
import java.util.concurrent.Callable;

public class DashboardFragment extends BaseFragment {

    private TabLayout tabNews;
    private DashboardViewModel model;
    //新闻
    private RecyclerView recNews;


    @Override
    protected int layoutId() {
        return R.layout.fragment_dashboard;
    }

    @Override
    protected void initView(View view) {

        //获得新闻实例
        recNews=view.findViewById(R.id.recNews);
        recNews.setLayoutManager(new GridLayoutManager(getContext(),1));

        tabNews=view.findViewById(R.id.tabNews);
        tabNews.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
             //   Toast.makeText(getContext(),tab.getTag().toString(),Toast.LENGTH_LONG).show();
                //根据新闻类型获得新闻
                String type=tab.getTag().toString();
                model.loadNews(type);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected void initData(Context context) {
        model=new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(DashboardViewModel.class);
        //新闻分类的方法
        initNewsCategory();
        //获得新闻
        initNews();
    }

    private void initNews() {
        model.getNews().observe(getViewLifecycleOwner(), new Observer<News>() {
            @Override
            public void onChanged(News news) {
                //获得所有新闻
                List<News.RowsBean> list=news.getRows();
                //将数据和组件绑定
                recNews.setAdapter(new NewsAdapter(list));

            }
        });
    }

    private void initNewsCategory() {
        model.getCategory().observe(getViewLifecycleOwner(), new Observer<Category>() {
            @Override
            public void onChanged(Category category) {
                //所有新闻分类
                List<Category.DataBean> list=category.getData();
               for(Category.DataBean d:list){
                   //创建一个tab
                   TabLayout.Tab tab=tabNews.newTab();
                   tab.setText(d.getName()); //显示的内容
                   tab.setTag(d.getId());   //隐藏的内容


                   tabNews.addTab(tab); //将tab添加进去
               }
            }
        });
    }
}